﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Zor_Modes;
using Zor_Bll;
using Zor_Tools; 
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace Zor_API.Controllers
{
    public class NoticeController : ApiController
    {
        /// <summary>
        /// 获取通知公告集合
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public List<M_Information> NoticeList()
        {
            List<M_Information> lst = Notice_Bll.GetNoticeList();
            return lst;
        }

        /// <summary>
        /// 新增通知公告
        /// </summary>
        /// <param name="infomation"></param>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage InsertNotice([FromBody]M_Information infomation)
        {
            BaseAnswer bar = Notice_Bll.AddNotice(infomation);

            HttpResponseMessage result = new HttpResponseMessage
            {
                Content = new StringContent(bar.ToJson(),System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
            };
            return result;
        }

        /// <summary>
        /// 获取信息详情
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public M_Information InfoMationById([FromBody]JObject obj)
        {
            if (obj == null) return new M_Information();
            var json = obj["id"].ToString();
            M_Information infomation = Notice_Bll.GetTById(Convert.ToInt32(json));
            return infomation;
        }

        /// <summary>
        /// 编辑通知公告
        /// </summary>
        /// <param name="infomation"></param>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage EditNotice([FromBody]M_Information infomation)
        {
            BaseAnswer bar = Notice_Bll.EditNotice(infomation);

            HttpResponseMessage result = new HttpResponseMessage
            {
                Content = new StringContent(bar.ToJson(), System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
            };
            return result;
        }

        /// <summary>
        /// 删除新闻公告
        /// </summary>
        /// <param name="infomation">id集合</param>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage DelNotice([FromBody]JObject infomation) 
        {
            dynamic json = infomation;

            JArray jnotice = json.infomation;
            List<int> lst = new List<int>();
            foreach (var item in jnotice)
            {
                lst.Add(item.ToObject<M_Information>().Info_Id);
            }
            BaseAnswer bar = Notice_Bll.DeleteNotice(lst.ToArray());
            HttpResponseMessage result = new HttpResponseMessage
            {
                Content = new StringContent(bar.ToJson(), System.Text.Encoding.GetEncoding("UTF-8"), "application/json")
            };
            return result; 

        }


    }
}
